#include "demo.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//"C:\\Users\\10602\\Desktop\\demo\\demo.xls"

static void graph_init(const void *in, void *out)
{
	struct vertex *v = (struct vertex *)in;
	v->edges = create_vector(struct edge);
	*(bool_t *)out = true;
}

static void graph_copy(const void *p1, const void *p2, void *out)
{
	memcpy((void*)p1, p2, sizeof(struct vertex));
	*(bool_t *)out = true;
}

static void graph_less(const void *p1, const void *p2, void *out)
{
	*(bool_t *)out = true;
}

static void graph_destroy(const void *in, void *out)
{
	struct vertex *v = (struct vertex *)in;
	vector_destroy(v->edges);
	*(bool_t *)out = true;
}

static void edges_init(const void *in, void *out)
{
	*(bool_t *)out = true;
}

static void edges_copy(const void *p1, const void *p2, void *out)
{
	memcpy((void*)p1, p2, sizeof(struct edge));
	*(bool_t *)out = true;
}

static void edges_less(const void *p1, const void *p2, void *out)
{
	*(bool_t *)out = true;
}

static void edges_destroy(const void *in, void *out)
{
	*(bool_t *)out = true;
}

struct vertex * find_vertex(vector_t* g, const char *id)
{
	struct vertex *p;
	iterator_t it;
	for (it = vector_begin(g); !iterator_equal(it, vector_end(g)); it = iterator_next(it))
	{
		p = (struct vertex *)iterator_get_pointer(it);
		if(!strcmp(id, p->vid)) return p;
	}
	return NULL;
}
/* original */
struct vertex* add_vertex(vector_t *g, struct vdata d)
{
	struct vertex v, *pv;
	bool_t t;
	vector_push_back(g, v);
	pv = vector_back(g);
	strcpy(pv->vid, d.id);
	strcpy(pv->vlabel, d.label);
	graph_init(pv, &t);
	return pv;	
}

struct vertex * add_edge(vector_t* g, const char *id1, const char *id2, float w, int time)
{
	struct vertex *pv1 = find_vertex(g, id1), *pv2 = find_vertex(g, id2);
	printf("%s-->%s	", id1, id2);
	if(pv1)
	{
		struct edge e, *pe;
		strcpy(e.eid, id2);
		strcpy(e.elabel, pv2->vlabel);
		e.w = w;
		e.time = time;
		vector_push_back(pv1->edges, &e);
		pe = vector_back(pv1->edges);
		printf("create airpot:%.0f time:%d\n", pe->w, pe->time);
	}
	return pv1;
}

void print_graph(vector_t *g)
{
	iterator_t iv, ie;
	struct vertex v;
	struct edge e;
	for (iv = vector_begin(g); !iterator_equal(iv, vector_end(g)); iv = iterator_next(iv))
	{
		iterator_get_value(iv, &v);
		printf("\n%s %s", v.vid, v.vlabel);
		for (ie = vector_begin(v.edges); !iterator_equal(ie, vector_end(v.edges)); ie = iterator_next(ie))
		{
			iterator_get_value(ie, &e);
			printf("-->%s %s %.0f %d", e.eid, e.elabel, e.w, e.time);
		}
		printf(";\n");
	}
}

int main(int argc, char *argv[])
{
	vector_t * vector;
	int *p, mun=9;
	vector=create_vector(int);
	p = &mun;
	vector_push_back(vector, *p);
	
	char start_station[6];
	char end_station[6];
	int J, Q, K;
	char *c, *n;
	struct vdata vertexes;
	struct edata edges;
	vector_t * graph;
	BookHandle book = xlCreateBook();//Define an Excel file
	
	if (book)
	{
		if (xlBookLoad(book, "demo.xls"))//Introducing Excel Files
		{
			SheetHandle sheet0 = xlBookGetSheet(book, 0);//Import worksheet one
			SheetHandle sheet1 = xlBookGetSheet(book, 1);//Import worksheet two
			type_register(struct edge, edges_init, edges_copy, edges_less, edges_destroy);
			type_register(struct vertex, graph_init, graph_copy, graph_less, graph_destroy);
			graph = create_vector(struct vertex);
			//Memory issues, later defined will erase the previous memory values, their memory is in the same address did not create a new address.
			if (sheet1)
			{
				Number_sites = 0;
				for(J=1; xlSheetReadStr(sheet1, J, 1, 0); J++)//Import the data for worksheet 2
				{
					strcpy(vertexes.id, xlSheetReadStr(sheet1, J, 1, 0));
					strcpy(vertexes.label, xlSheetReadStr(sheet1, J, 2, 0));
					add_vertex(graph, vertexes);
					Number_sites++;
				}
				
				for(J=1; xlSheetReadStr(sheet0, J, 2, 0); J++)//Import the data for worksheet 1
				{
					//for (Q = 1; Q; Q++)
					//{
						edges.w = (float)xlSheetReadNum(sheet0, J, 1, 0);//bus number
						strcpy(edges.id1, xlSheetReadStr(sheet0, J, 2, 0));//station_start
						strcpy(edges.id2, xlSheetReadStr(sheet0, J, 3, 0));//destination
						edges.time = xlSheetReadNum(sheet0, J, 4, 0);//time
						add_edge(graph, edges.id1, edges.id2, edges.w, edges.time);
						//printf("%s %s %.0f\n", edges.id1, edges.id2, edges.w);
						//add_edge(graph, edges.id2, edges.id1, edges.w);
					//}
				}
				
			}
			else
			{
				printf("Error for \"sheet\".");
				return -1;
			}
		}
		else
		{
			printf("docment have error.");
			return -1;
		}
	}
	xlBookRelease(book);
	
	print_graph(graph);//Output the linked list created
	printMatrix(graph);//Output connection matrix
	matrix_power(graph);
	
	do{
		printf("Please enter start station:\n");
		scanf("%s", start_station);	
		if (strcmp(start_station, "q") == 0)
		{
			return 0;
		}
		
		printf("Please enter end station:\n");
		scanf("%s", end_station);
		if (strcmp(end_station, "q") == 0)
		{
			return 0;
		}
		
		if(strcmp(start_station, end_station) != 0)
		{
			printf("Ordinary search:\n");
		}
		else
		{
			printf("Are you bored?\n");
			continue;
		}
	} while(ordinary_search(graph, start_station, end_station));
	vector_destroy(graph);//Destruction of the list
	getchar();

	return 0;
}